<template>
  <el-card>
    <div class="search-input-group">
      <el-form :model="searchForm" :inline="true">
        <el-form-item style="padding:5px" label="商品编号">
          <el-input 
            v-model="searchForm.productId" 
            placeholder="请输入商品编号" 
            clearable
          />
        </el-form-item>
        <el-form-item style="padding:5px" label="状态">
          <el-select v-model="searchForm.dataStatus" placeholder="请选择状态" clearable>
            <el-option label="显示" value="1"></el-option>
            <el-option label="隐藏" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleResetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    
    <div class="btn-group">
      <el-button 
        v-if="btnGroup.addb" 
        type="primary" 
        :icon="Plus"
        @click="showAddDialog"
      >
        添加
      </el-button>
      <el-button 
        v-if="btnGroup.editb" 
        type="success" 
        :icon="Edit"
        @click="showEditDialog"
        :disabled="multipleSelection.length !== 1"
      >
        修改
      </el-button>
      <el-button 
        v-if="btnGroup.deleteb" 
        type="danger" 
        :icon="Delete"
        @click="handleDelete"
        :disabled="multipleSelection.length <= 0"
      >
        删除
      </el-button>
    </div>
    
    <el-table 
      :data="dataList" 
      style="width: 100%" 
      row-key="id"
      @selection-change="handleSelectionChange"
      border
    >
      <el-table-column type="selection" width="40" />
      <el-table-column type="index" label="序号" width="80" show-overflow-tooltip/>
      <el-table-column prop="id" label="数据编号" width="100" show-overflow-tooltip/>
      <el-table-column prop="userId" label="用户编号" width="100" show-overflow-tooltip/>
      <el-table-column prop="productNum" label="商品编号" width="100" show-overflow-tooltip/>
      <el-table-column prop="evaluationContent" label="评论内容" width="200" show-overflow-tooltip/>
      <el-table-column prop="dataStatus" label="状态" width="100" show-overflow-tooltip>
        <template #default="scope">
          <el-tag :type="scope.row.dataStatus === 1 ? 'success' : 'danger'">
            {{ scope.row.dataStatus === 1 ? '显示' : '隐藏' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180" show-overflow-tooltip />
      <el-table-column prop="updateTime" label="更新时间" width="180" show-overflow-tooltip />
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      v-model:current-page="pagination.currentPage"
      v-model:page-size="pagination.pageSize"
      :total="pagination.total"
      :page-sizes="[10, 20, 50]"
      layout=" prev, pager, next"
      background
      @current-change="handleCurrentChange"
      class="pagination"
    />

    <!-- 添加/修改对话框 -->
    <el-dialog
      :title="dialogTitle"
      v-model="dialogVisible"
      width="500px"
      @close="handleDialogClose"
    >
      <el-form
        :model="operateDataForm"
        :rules="operateDataFormRules"
        ref="operateDataFormRef"
        label-width="100px"
      >
        <el-form-item label="标题" prop="bannerTitle">
          <el-input 
            v-model="operateDataForm.bannerTitle" 
            placeholder="请输入标题"
          />
        </el-form-item>
        <el-form-item label="描述" prop="bannerDesc">
          <el-input 
            v-model="operateDataForm.bannerDesc" 
            placeholder="请输入描述"
            type="textarea"
            :rows="3"
          />
        </el-form-item>
        <el-form-item label="商品编号" prop="productId">
          <el-input 
            v-model="operateDataForm.productId" 
            placeholder="请输入商品编号"
            type="number"
          />
        </el-form-item>
        <el-form-item label="状态" prop="dataStatus" v-if="isEdit">
          <el-select v-model="operateDataForm.dataStatus" placeholder="请选择状态">
            <el-option label="显示" value="1"></el-option>
            <el-option label="隐藏" value="0"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitForm">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </el-card>
</template>

<script setup>
import {
  Plus,
  Edit,
  Delete
} from '@element-plus/icons-vue'
import { getList, getSearchList, add, edit, remove } from "@/api/common/base"
import { onMounted, reactive, ref, computed } from "vue"

// api前缀
const API_PREFIX = "/admin/evaluation"

// 对话框标题
const dialogTitle = computed(() => {
  return isEdit.value ? '修改轮播图' : '添加轮播图'
})

// 搜索表单
const searchForm = reactive({
  productId: '',
  dataStatus: ''
})

// 对话框相关数据
const dialogVisible = ref(false)
const isEdit = ref(false)
const operateDataFormRef = ref(null)

// 添加或修改数据表单
const operateDataForm = reactive({
  id: '',
  bannerTitle: '',
  bannerDesc: '',
  productId: '',
  dataStatus: '1'
})

// 表单验证规则
const operateDataFormRules = {
  bannerTitle: [
    { required: true, message: '请输入Banner标题', trigger: 'blur' },
    { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur' }
  ],
  bannerDesc: [
    { required: true, message: '请输入Banner描述', trigger: 'blur' },
    { min: 2, max: 200, message: '长度在 2 到 200 个字符', trigger: 'blur' }
  ],
  productId: [
    { required: true, message: '请输入商品编号', trigger: 'blur' },
    { pattern: /^[0-9]+$/, message: '请输入有效的数字', trigger: 'blur' }
  ],
  dataStatus: [
    { required: true, message: '请选择状态', trigger: 'change' }
  ]
}

// 显示添加对话框
const showAddDialog = () => {
  isEdit.value = false
  dialogVisible.value = true
  // 重置表单
  Object.assign(operateDataForm, {
    id: '',
    bannerTitle: '',
    bannerDesc: '',
    productId: '',
    dataStatus: '1'
  })
}

// 显示编辑对话框
const showEditDialog = () => {
  isEdit.value = true
  dialogVisible.value = true
  // 填充表单数据
  Object.assign(operateDataForm, multipleSelection.value[0])
}

// 对话框关闭时的处理
const handleDialogClose = () => {
  operateDataFormRef.value.resetFields()
}

// 固定参数源
const dataList = ref([])
const multipleSelection = ref([])
const isSearch = ref(false)
const btnGroup = reactive({
  addb: false,
  editb: false,
  deleteb: false
})
const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0
})

// 获取数据列表
const getDataList = () => {
  let res = getList(API_PREFIX, pagination.currentPage)
  res.then(res => {
    dataList.value = res.data.records
    let opeStr = res.operateStr
    if (opeStr.includes("*") || opeStr.includes("a")) btnGroup.addb = true
    if (opeStr.includes("*") || opeStr.includes("d")) btnGroup.deleteb = true
    if (opeStr.includes("*") || opeStr.includes("u")) btnGroup.editb = true
    pagination.total = res.data.total
    pagination.pageSize = res.data.size
    pagination.currentPage = res.data.current
    console.log(res.data);
  })
}

// 搜索处理
const handleSearch = () => {
  isSearch.value = true
  let res = getSearchList(API_PREFIX, { 
    productId: searchForm.productId,
    dataStatus: searchForm.dataStatus
  }, pagination.currentPage)
  res.then(res => {
    dataList.value = res.data.records
    pagination.total = res.data.total
    pagination.pageSize = res.data.size
    pagination.currentPage = res.data.current
  })
}

// 重置搜索
const handleResetSearch = () => {
  searchForm.productId = ""
  searchForm.dataStatus = ""
  isSearch.value = false
  getDataList()
}

// 提交表单
const submitForm = () => {
  operateDataFormRef.value.validate(async (valid) => {
    if (valid) {
      // 转换状态值为数字类型
      const formData = {
        bannerTitle: operateDataForm.bannerTitle,
        bannerDesc: operateDataForm.bannerDesc,
        productId: operateDataForm.productId,
        dataStatus: parseInt(operateDataForm.dataStatus)
      };
      
      if (isEdit.value) {
        formData.id = operateDataForm.id;
        await edit(API_PREFIX, formData)
      } else {
        await add(API_PREFIX, formData)
      }
      dialogVisible.value = false
      refreshList()
    }
  })
}

// 删除操作
const handleDelete = async () => {
  pagination.currentPage = 1
  const ids = multipleSelection.value.map(item => item.id)
  await remove(API_PREFIX, { list: ids })
  refreshList()
}

// 处理选择变化
const handleSelectionChange = (selection) => {
  multipleSelection.value = selection
}

// 当前页改变时触发
const handleCurrentChange = (val) => {
  pagination.currentPage = val
  refreshList()
}

// 刷新列表
const refreshList = () => {
  if (isSearch.value) handleSearch()
  else getDataList()
}

onMounted(() => {
  getDataList()
})
</script>

<style scoped>
.dialog-footer {
  display: flex;
  justify-content: flex-end;
}

.btn-group {
  margin-bottom: 15px;
}

.btn-group .el-button {
  margin-right: 1px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}

.search-input-group {
  margin-bottom: 15px;
  padding: 15px;
  background-color: #f5f7fa;
  border-radius: 4px;
}

.search-input-group .el-form-item {
  margin-bottom: 0;
  margin-right: 15px;
}
</style>